
Partial Class Admin_PermissionsByType
    Inherits System.Web.UI.Page

    Public Property PermissionList() As IList
        Get
            Return ViewState("collectionlist")
        End Get
        Set(ByVal value As IList)
            ViewState("collectionlist") = value
        End Set
    End Property

    Public Property TopLevelPermission() As Cdm.DataCore.PermissionObject
        Get
            Return ViewState("PermissionObject")
        End Get
        Set(ByVal value As Cdm.DataCore.PermissionObject)
            ViewState("PermissionObject") = value
        End Set
    End Property

    Protected Sub ddlRoles_SelectedIndexChanged(ByVal sender As Object, _
                                                ByVal e As System.EventArgs) _
                                                Handles RolesDropDownList.SelectedIndexChanged
        SetDescription()
        BindGrid()
    End Sub

    Protected Sub CheckSelectAllCheckBox()

        Dim hasPermissionCheckBox As CheckBox
        Dim selectAllCheckBox As CheckBox = CType(Me.PermissionsScrollingGridView.HeaderRow.Cells(2).FindControl("SelectAllCheckBox"), CheckBox)
        If selectAllCheckBox IsNot Nothing Then

            For Each row As GridViewRow In Me.PermissionsScrollingGridView.Rows

                If row.RowType = DataControlRowType.DataRow Then

                    hasPermissionCheckBox = CType(row.Cells(2).FindControl("HasPermissionCheckBox"), CheckBox)
                    If hasPermissionCheckBox IsNot Nothing Then
                        If Not hasPermissionCheckBox.Checked Then
                            selectAllCheckBox.Checked = False
                            Exit Sub
                        End If
                    End If

                End If

            Next

            selectAllCheckBox.Checked = True

        End If

    End Sub

    Private Sub BindGrid()

        TopLevelPermission = Nothing
        PermissionList = Nothing

        Me.GridObjectDataSource.DataObjectTypeName = Me.PermissionTypesDropDownList.SelectedValue
        Me.GridObjectDataSource.TypeName = Me.PermissionTypesDropDownList.SelectedValue
        Me.GridObjectDataSource.SelectMethod = "ListBySort"
        'Me.PermissionsScrollingGridView.Columns(0).SortExpression = GetSortExpression()
        Me.GridObjectDataSource.Select()
        Me.PermissionsScrollingGridView.DataBind()

    End Sub

    Private Function GetSortExpression() As String
        Select Case Me.PermissionTypesDropDownList.SelectedItem.Text.ToUpper
            Case "REPORTS"
                Return "ReportShortName"
            Case "FORMS"
                Return "FormShortName"
            Case "ALERT"
                Return "AlertSubject"
            Case "NOTIFICATION"
                Return "NotificationSubject"
            Case "FILE NOTE TYPE"
                Return "Description"
        End Select
        Return ""
    End Function

    Private Function PermissionExists(ByVal permissionId As Integer) As Boolean

        Dim i As Integer
        Dim colPermission As IList = PermissionList()
        Dim objPermissionRole As Cdm.DataCore.PermissionRoleObject
        If colPermission Is Nothing Then
            Return False
        End If
        For i = 0 To colPermission.Count - 1
            objPermissionRole = colPermission(i)
            If objPermissionRole.PermissionId = permissionId Then
                Return True
            End If
        Next

        Return False

    End Function

   

    Protected Sub SaveButton_Click(ByVal sender As Object, _
                                   ByVal e As System.EventArgs) _
                                   Handles SaveButton.Click

        Dim objPermission As Cdm.DataCore.PermissionObject = GetPermissionObject()
        PermissionList = objPermission.PermissionsByRoleId(CInt(RolesDropDownList.SelectedValue))

        Dim i As Integer
        For i = 0 To PermissionsScrollingGridView.Rows.Count - 1
            Dim chkPermission As CheckBox = PermissionsScrollingGridView.Rows(i).FindControl("HasPermissionCheckBox")
            If Not chkPermission Is Nothing Then
                Dim permissionId As Integer = CInt(Me.PermissionsScrollingGridView.Rows(i).Cells(0).Text)
                SetPermission(permissionId, chkPermission.Checked)
            End If
        Next

        SetStatus("Settings Saved.")

    End Sub

    Private Sub SetPermission(ByVal permissionId As Integer, _
                              ByVal newPermission As Boolean)

        If newPermission = True And PermissionExists(permissionId) = False Then
            Me.TopLevelPermission.AddPermission(CInt(Me.RolesDropDownList.SelectedValue), permissionId)
        ElseIf newPermission = False And PermissionExists(permissionId) = True Then
            RemovePermission(permissionId)
        End If

    End Sub


    Private Sub RemovePermission(ByVal idValue As Integer)

        Dim i As Integer
        For i = 0 To PermissionList().Count
            Dim permissionRole As Cdm.DataCore.PermissionRoleObject = PermissionList(i)
            If permissionRole.PermissionId = idValue Then
                permissionRole.Remove()
                Exit Sub
            End If
        Next

    End Sub


    Protected Sub ResetButton_Click(ByVal sender As Object, _
                                    ByVal e As System.EventArgs) _
                                    Handles ResetButton.Click

        BindGrid()
        SetStatus("Settings have been reset.")

    End Sub

    Protected Sub ddlPermissionTypes_DataBound(ByVal sender As Object, _
                                               ByVal e As System.EventArgs) _
                                               Handles PermissionTypesDropDownList.DataBound

        If Not Request("PermissionType") Is Nothing Then
            SetDropDown()
        End If

        If Not Page.IsPostBack Then
            SetDescription()
        End If

    End Sub

    Protected Sub ddlPermissionTypes_SelectedIndexChanged(ByVal sender As Object, _
                                                          ByVal e As System.EventArgs) _
                                                          Handles PermissionTypesDropDownList.SelectedIndexChanged

        SetDescription()
        BindGrid()

    End Sub

    Private Sub SetDescription()

        If Not Me.PermissionTypesDropDownList.SelectedItem Is Nothing Then
            Dim selectedPermission As Cdm.DataCore.LookupTables.LuAdminTableList
            selectedPermission = Cdm.DataCore.LookupTables.LuAdminTableList.Retrieve(Me.PermissionTypesDropDownList.SelectedItem.Text)
            Me.PermissionsScrollingGridView.Caption = selectedPermission.Description & " (Selected Role: " & Me.RolesDropDownList.SelectedItem.Text & ")"
        End If

    End Sub

    Private Sub SetDropDown()

        Dim selectedPermissionType As String = Request("PermissionType").ToString().ToUpper()
        Dim i As Integer
        For i = 0 To Me.PermissionTypesDropDownList.Items.Count - 1
            If Me.PermissionTypesDropDownList.Items(i).Text.ToUpper().Equals(selectedPermissionType) Then
                Me.PermissionTypesDropDownList.SelectedIndex = i
                SetDescription()
                BindGrid()
                Exit Sub
            End If
        Next

    End Sub


    Private Function GetPermissionObject() As Cdm.DataCore.PermissionObject
        Dim roleId As Integer = Me.RolesDropDownList.SelectedValue

        Select Case Me.PermissionTypesDropDownList.SelectedValue
            Case "Cdm.DataCore.CaseNotes.Notification"
                Return New Cdm.DataCore.CaseNotes.Notification
            Case "Cdm.DataCore.CaseNotes.Alert"
                Return New Cdm.DataCore.CaseNotes.Alert
            Case "Cdm.DataCore.CaseNotes.Reports"
                Return New Cdm.DataCore.CaseNotes.Reports
            Case "Cdm.DataCore.CaseNotes.Forms"
                Return New Cdm.DataCore.CaseNotes.Forms
            Case "Cdm.DataCore.LookupTables.LuCaseNoteType"
                Return New Cdm.DataCore.LookupTables.LuCaseNoteType
        End Select
        Return Nothing
    End Function


    Protected Sub PermissionsScrollingGridView_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles PermissionsScrollingGridView.DataBound
        If Me.PermissionsScrollingGridView.Rows.Count > 0 Then
            CheckSelectAllCheckBox()
        End If
        AccessibilityController.AddImageToSortableGridHeaders(PermissionsScrollingGridView)
    End Sub

    Protected Sub PermissionsScrollingGridView_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles PermissionsScrollingGridView.RowCreated

        Dim chkPermission As CheckBox = e.Row.FindControl("HasPermissionCheckBox")
        Dim objPermission As Cdm.DataCore.PermissionObject = e.Row.DataItem
        If objPermission Is Nothing Then Exit Sub
        If TopLevelPermission Is Nothing Then TopLevelPermission = objPermission
        If PermissionList() Is Nothing Then
            PermissionList = objPermission.PermissionsByRoleId(CInt(RolesDropDownList.SelectedValue))
        End If

        Dim id As Integer = objPermission.Id
        If Not chkPermission Is Nothing Then
            chkPermission.Checked = PermissionExists(id)
        End If
    End Sub

    Protected Sub PermissionsScrollingGridView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles PermissionsScrollingGridView.RowDataBound
        'e.Row.Cells(0).Style("display") = "none"
        Dim permissionCheckBox As CheckBox = e.Row.FindControl("HasPermissionCheckBox")

        Dim currentPermission As Cdm.DataCore.PermissionObject = CType(e.Row.DataItem, Cdm.DataCore.PermissionObject)
        If currentPermission IsNot Nothing Then
            Dim permissionName As String = currentPermission.AdminDescription
            If permissionCheckBox IsNot Nothing Then
                permissionCheckBox.InputAttributes.Add("title", "Receives Communication - " & permissionName)
            End If
        End If

    End Sub

    Protected Sub PermissionsScrollingGridView_Sorted(ByVal sender As Object, ByVal e As System.EventArgs) Handles PermissionsScrollingGridView.Sorted

        Dim permissionName As String
        Dim permissionCheckBox As CheckBox
        Dim ID As Integer

        For i As Integer = 0 To PermissionsScrollingGridView.Rows.Count - 1
            Dim dblc As System.Web.UI.DataBoundLiteralControl = PermissionsScrollingGridView.Rows(i).Cells(1).Controls(0)
            permissionName = dblc.Text
            ID = PermissionsScrollingGridView.Rows(i).Cells(0).Text
            permissionCheckBox = PermissionsScrollingGridView.Rows(i).FindControl("HasPermissionCheckBox")
            If permissionCheckBox IsNot Nothing Then
                permissionCheckBox.Checked = PermissionExists(ID)
                permissionCheckBox.InputAttributes.Add("title", "Receives Communication - " & permissionName)
            End If
        Next
    End Sub

    Protected Sub PermissionsScrollingGridView_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles PermissionsScrollingGridView.Sorting
        Me.GridObjectDataSource.DataObjectTypeName = Me.PermissionTypesDropDownList.SelectedValue
        Me.GridObjectDataSource.TypeName = Me.GridObjectDataSource.DataObjectTypeName

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        SetupJavaScript()
    End Sub

    Private Sub SetupJavaScript()

        EntityController.JavaScriptAddChildDiv(Me.PermissionsScrollingGridView.DivClientId, CInt(Me.PermissionsScrollingGridView.ToolTip))
        ClientScript.RegisterStartupScript(Me.GetType(), "scrollDivs", EntityController.JavaScriptScrollingGridStartup(Me.PermissionsScrollingGridView), True)
        EntityController.JavaScriptSetFixedHeight(28)
        ScriptManager.RegisterClientScriptBlock(Me.PermissionsScrollingGridView, Me.PermissionsScrollingGridView.GetType(), _
        "scrollDivAsync", EntityController.JavaScriptScrollingGridStartup(Me.PermissionsScrollingGridView), True)
    End Sub

    Private Sub ClearStatus()
        SetStatus("")

    End Sub
    Private Sub SetStatus(ByVal message As String)
        Dim mymaster As ASP.admin_admin_master = CType(Me.Master, ASP.admin_admin_master)
        mymaster.SetStatus(message)
    End Sub

End Class
